package bgu.eliav;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class Class4 extends Activity implements TimePickerDialog.OnTimeSetListener {
	/** Called when the activity is first created. */
	final int SUB_MENU_1 = 13;
	final int SUB_MENU_2 = 14;
	final int SUB_MENU_3 = 15;
	final int SUB_MENU_4 = 16;
	final int SUB_MENU_21 = 17;
	final int SUB_MENU_22 = 18;

	private TextView display;
	private TextView time;
	private TextView timeText;
	String alertDialogMsg = new String("");
	private int mHour = 0;
	private int mMinute = 0;


	static final int DIALOG_PAUSED_ID = 0;
	static final int DIALOG_ALERT = 1;
	static final int DIALOG_CUSTOM = 2;
	private static final int EDIT_ID = 3;
	private static final int DELETE_ID = 4;
	private static final int TIME_DIALOG_ID = 5;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		display = (TextView)findViewById(R.id.display);
		registerForContextMenu(display);
		time = (TextView)findViewById(R.id.timeDisplay);
		registerForContextMenu(time);
		timeText = (TextView)findViewById(R.id.TextView01);
		registerForContextMenu(timeText);		
	}

	@Override 
	public boolean onCreateOptionsMenu(Menu menu) { 
		super.onCreateOptionsMenu(menu); 
		MenuInflater inflater = getMenuInflater(); 
		inflater.inflate(R.menu.menu, menu); 

		// add menu item with listener
		int group = 2;
		MenuItem mi = menu.add(group, 10, 10, "pause");
		final TextView tv = display;
		mi.setOnMenuItemClickListener(new OnMenuItemClickListener(){
			public boolean onMenuItemClick(MenuItem arg0) {
				tv.setText("menu item 21 - setOnMenuItemClickListener");
				Log.d("Class4","menu item 21 - setOnMenuItemClickListener");
				showDialog(DIALOG_PAUSED_ID);
				return true;
			}
		});

		// add sub menus
		SubMenu subMenu = menu.addSubMenu(group, 11, 11, "sub menu");
		subMenu.add(group, SUB_MENU_1, SUB_MENU_1, "sub 1");
		subMenu.add(group, SUB_MENU_2, SUB_MENU_2, "sub 2");
		subMenu.add(group, SUB_MENU_3, SUB_MENU_3, "sub 3");
		subMenu.add(group, SUB_MENU_4, SUB_MENU_4, "sub 4");
		subMenu.setGroupCheckable(group, true, true);
		subMenu = menu.addSubMenu(group, 12, 12, "sub menu 2");
		mi = subMenu.add(group, SUB_MENU_21, SUB_MENU_21, "sub 21");
		mi.setCheckable(true);
		mi = subMenu.add(group, SUB_MENU_22, SUB_MENU_22, "sub 22");
		mi.setCheckable(true);
		return true;	
	} 

	@Override 
	public boolean onOptionsItemSelected(MenuItem item) { 
		switch (item.getItemId()) { 
		case R.id.item11: 
			display.setText("menu item 11 selected");
			Log.d("Class4","menu item 11 selected");
			showDialog(DIALOG_ALERT);
			return true; 
		case R.id.item12: 
			display.setText("menu item 12 selected");
			Log.d("Class4","menu item 12 selected");
			ProgressDialog.show(this, "progress dialog", "menu item 12 selected")
			.setCancelable(true);
			return true; 
		case R.id.item13: 
			display.setText("menu item 13 selected");
			Log.d("Class4","menu item 13 selected");
			showDialog(DIALOG_CUSTOM);
			return true; 
		case R.id.item63: 
			display.setText("s menu item from s menue 3 selected");
			Log.d("Class4","s menu item from s menue 3 selected");
			return true; 
		case SUB_MENU_1: 
			display.setText("SUB_MENU_1 selected");
			Log.d("Class4","SUB_MENU_1 selected");
			if (item.isChecked()) item.setChecked(false);
			else item.setChecked(true);
			return true; 
		case SUB_MENU_2: 
			display.setText("SUB_MENU_2 selected");
			Log.d("Class4","SUB_MENU_2 selected");
			if (item.isChecked()) item.setChecked(false);
			else item.setChecked(true);
			return true; 
		case SUB_MENU_3: 
			display.setText("SUB_MENU_3 selected");
			Log.d("Class4","SUB_MENU_3 selected");
			if (item.isChecked()) item.setChecked(false);
			else item.setChecked(true);
			return true; 
		case SUB_MENU_4: 
			display.setText("SUB_MENU_4 selected");
			Log.d("Class4","SUB_MENU_4 selected");
			if (item.isChecked()) item.setChecked(false);
			else item.setChecked(true);
			return true; 
		case SUB_MENU_21: 
			display.setText("SUB_MENU_21 selected");
			Log.d("Class4","SUB_MENU_21 selected");
			if (item.isChecked()) item.setChecked(false);
			else item.setChecked(true);
			return true; 
		case SUB_MENU_22: 
			display.setText("SUB_MENU_22 selected");
			Log.d("Class4","SUB_MENU_22 selected");
			if (item.isChecked()) item.setChecked(false);
			else item.setChecked(true);
			return true; 
			// More items go here (if any) ... 
		} 
		return super.onOptionsItemSelected(item); 
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		if (v == time || v == timeText){
			showDialog(TIME_DIALOG_ID);
		}else if (v == display){
			menu.add(0, EDIT_ID, 0, "Edit");
			menu.add(0, DELETE_ID, 0,  "Delete");
			menuInfo = new AdapterContextMenuInfo(v,0,0);
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case EDIT_ID:
			alertDialogMsg = "edit " + display.getText().toString();
			display.setText("edited!!!");
			showDialog(DIALOG_ALERT);
			return true;
		case DELETE_ID:
			display.setText("");
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}


	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;;
		switch(id) {
		case DIALOG_PAUSED_ID:
			return creatingPauseDialog();
		case DIALOG_ALERT:
			return creatingAlertDialog("This is an Alert Dialog ...");
		case DIALOG_CUSTOM:
			return creatingCustomDialog("This is acustom Dialog ...");
		case TIME_DIALOG_ID:
			return new TimePickerDialog(this,this, mHour, mMinute, false);
		default:
			dialog = null;
		}
		return dialog;
	}

	public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
		mHour = hourOfDay;
		mMinute = minute;
		updateTimeDisplay();
	}

	private void updateTimeDisplay() {
		time.setText(new StringBuilder()
		.append(pad(mHour)).append(":")
		.append(pad(mMinute)));
	}

	private static String pad(int c) {
		if (c >= 10)
			return String.valueOf(c);
		else
			return "0" + String.valueOf(c);
	}

	private Dialog creatingPauseDialog()
	{
		final Activity localActivity = this;
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Are you sure you want to exit?")
		.setCancelable(false)
		.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				localActivity.finish();
			}
		})
		.setNegativeButton("No", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});
		AlertDialog alert = builder.create();
		return alert;
	}

	private Dialog creatingAlertDialog(String msg)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(msg)
		.setCancelable(true)
		.setTitle("Alert!");
		AlertDialog alert = builder.create();
		return alert;
	}

	@Override
	protected void onPrepareDialog(int id,Dialog dialog) {
		if (id == DIALOG_ALERT){
			dialog.setTitle(alertDialogMsg);
		}
	}

	private Dialog creatingCustomDialog(String msg)
	{
		Dialog dialog = new Dialog(this);
		dialog.setContentView(R.layout.custom_dialog);
		dialog.setTitle("Custom Dialog");
		TextView text = (TextView) dialog.findViewById(R.id.text);
		text.setText(msg);
		ImageView image = (ImageView) dialog.findViewById(R.id.image);
		image.setImageResource(R.drawable.icon);
		return dialog;
	}
}